import express from 'express'
import { body, query } from 'express-validator'

import validatorMiddleware from '#src/middleware/validator.js'

const router = express.Router()

const addRules = [
  body('roleName')
    .trim()
    .notEmpty()
    .withMessage('角色名称不能为空')
    .isLength({ max: 40 })
    .withMessage('角色名称最大40字符'),
  body('menuList').optional({ values: 'falsy' }).isArray().withMessage('请传入合法 menuList'),
  body('desc').trim().optional({ values: 'falsy' }).isLength({ max: 120 }).withMessage('角色描述最多120字符')
]

const updateRules = addRules.concat([body('roleId').notEmpty().withMessage('角色id不能为空')])

import {
  roleList,
  createRole,
  updateRole,
  deleteRole,
  detailRole,
  getMenuListByRole,
  updateRoleStatus,
  getAllRoleList
} from '../controllers/roleController/index.js'

router.post('/list', roleList)

/**
 * 新增角色
 */
router.post('/create', validatorMiddleware(addRules), createRole)

/**
 * 更新角色
 * 更新角色上的权限
 * 更新角色基础信息-角色名称
 */
router.post('/update', validatorMiddleware(updateRules), updateRole)

router.delete('/delete', validatorMiddleware([query('id').notEmpty().withMessage('请传入角色id')]), deleteRole)

router.get('/detail', detailRole)

/**
 * 根据用户信息查询菜单
 */
router.get('/getMenuListByRole', getMenuListByRole)

/**
 * 更新角色状态
 */
router.post(
  '/updateRoleStatus',
  validatorMiddleware([
    body('roleId').notEmpty().withMessage('角色roleId不能为空'),
    body('status').notEmpty().withMessage('状态不能为空').isIn([0, 1]).withMessage('类型不符， 仅支持 0,1')
  ]),
  updateRoleStatus
)

router.get('/getAllRoleList', getAllRoleList)

export default router
